Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/testing #81

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open

Feat/testing #81

wants to merge 20 commits into from

Conversation

sofisdev
Copy link
Contributor

@sofisdev sofisdev commented Aug 3, 2021

Type:

  • cicd: helm, docker & cicd adjustments.
  • feature: new capabilities.
  • fix: bug, hotfix, etc.
  • refactor: enhancements.
  • style: changes in styles.
  • other: docs, tests.

What's the focus of this PR:

  • Focus on validating the JSON file to check if it fits the criteria for the FormBuilder component
  • We still need to define what happens when the JSON file does not match the Joi Schema

Copy link

@totegsito totegsito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing here job @sofisdev !

I have added a couple of suggestions. Take a look at them and let me know what you think 😄

questions: Joi.array()
.required()
.items(
Joi.object({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cool if this declaration would be in a separate file (maybe QuestionSchemas could fit) and reused on dependentQuestions instead of duplicating 😬

Also, I have read the README and it looks like there are different kind of questions with different properties. It would be amazing if each kind of question will have it own schema.

Something like this:

// baseQuestionSchema.js

const question = Joi.object({
  // This way it can have recursive validations
  dependentQuestion: Joi.array().items(Joi.lazy(() => question))
})

// checkBoxSchema.js
import questionSchema from './baseQuestionSchema.js'

const checkBoxSchema = questionSchema.append({
// checkBox specific properties

})

// questionsSchema.js
const questionsSchema = Joi.array().items(CheckBoxSchema, CountrySchema, InputSchema, ...etc)

// jsonValidator.js

questions: questionsSchema.required()

This way, each Question update would be independent of the others and they would be easier to test 😬

What do you believe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm loving this approach, so well structured!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the approach since Joi.lazy() and Joi.link() were not a solution, however I still have a bug I'm currently trying to solve

src/builder.js Outdated
@@ -36,6 +37,7 @@ const FormBuilder = ({
isoCode,
...props
}) => {
console.log(schema.validate(form))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better if instead of logging an error it will throw in case the schema is incorrect, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, this was only temporary until we decide what would be rendered in case of error

id: Joi.string().required()
})

export default schema

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cause this file is a jsonValidator, what if instead of returning Joi's schemas it returns a validate function which uses Joi.validate. This way we'll be using a Joi's wrapper and we could change the library when required.

Something like this:

const validate = (json) => {
  const result = schema.validate(json);
  if (result.error) {
    throw new Error(`Validation errors: ${result.error}`)
  }
  return result.value
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it!

Copy link
Contributor

@ardguezsoc ardguezsoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice job 😄 💥

package.json Outdated Show resolved Hide resolved
src/__tests__/builder.test.js Show resolved Hide resolved
src/utils/jsonValidator.js Outdated Show resolved Hide resolved
src/utils/jsonValidator.js Outdated Show resolved Hide resolved
Copy link
Contributor

@ismaelocaramelo ismaelocaramelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @sofisdev I 've left some comments that would be nice to go through them. :)

@@ -181,6 +181,7 @@ const App = () => {
isoCode='ES'
onLinkOpen={onLinkOpen}
isLoading={isLoading}
validateJSON
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid truly expressions. The reason behind this is if I'm passing to boolean expressions let say validateJSON and notValidateJSON I would not know the behavior without going to the implementation details. Think components as functions, passing booleans as arguments makes the function really hard to maintain. I'd use some sort of state handling @sofisdev

window.MutationObserver = MutationObserver

let component = null
let mockHandler = null
// const isLoading = false
let loaderc = false
// function useLoading(isLoading) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comments

...props
}) => {
const useFormObj = useForm({ defaultValues: { formatDate: '' } })
const { errors } = useFormObj

validateJSON && validate(form)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we handle this with try and catch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants